import datetime
import re

IDCARD_REGEX = '[1-9][0-9]{14}([0-9]{2}[0-9X])?'


def is_valid_idcard(idcard):
    if isinstance(idcard, int):
        idcard = str(idcard)
    if not re.match(IDCARD_REGEX, idcard):
        return False
    items = [int(item) for item in idcard[:-1]]
    ## 加权因子表
    factors = (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2)
    ## 计算17位数字各位数字与对应的加权因子的乘积
    copulas = sum([a * b for a, b in zip(factors, items)])
    ## 校验码表
    ckcodes = ('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2')
    print(ckcodes[copulas % 11].upper() == idcard[-1].upper())
    return ckcodes[copulas % 11].upper() == idcard[-1].upper()


# 手机号正则匹配
def is_phone(phone):
    phone_pat = re.compile('^(13\d|14[5|7]|15\d|166|17[3|6|7]|18\d)\d{8}$')
    res = re.search(phone_pat, phone)
    if not res:
        return False
    return True


# 验证码验证
def is_code(code):
    code_pat = re.compile('\d{6}')
    res = re.search(code_pat, code)
    if not res:
        return False
    return True


# 年龄验证
def is_check(start, end, practical):
    if int(start) < int(practical) and int(practical) < int(end):
        return True
    else:
        return False


# 性别检验
def is_sex(a, b):
    if int(a) == 0:
        return True
    else:
        if int(a) == int(b):
            return True
        else:
            return False


# 判断是否在可预约时间范围内
def verify_datetime(start, end, time):
    from datetime import datetime
    if datetime.strptime(time, '%Y-%m-%d') > datetime.strptime(
            start, '%Y-%m-%d') and datetime.strptime(time, '%Y-%m-%d') < datetime.strptime(end, '%Y-%m-%d'):
        print('1')
        return True
    else:
        print('2')
        return False


# 获取预约那天是星期几
def verify_week(time):
    import datetime
    week = datetime.datetime.strptime(time, "%Y-%m-%d").weekday() + 1
    if week == 1:
        print('星期一')
        return '星期一'
    elif week == 2:
        print('星期二')
        return '星期二'
    elif week == 3:
        print('星期三')
        return '星期三'
    elif week == 4:
        print('星期四')
        return '星期四'
    elif week == 5:
        print('星期五')
        return '星期五'
    elif week == 6:
        print('星期六')
        return '星期六'
    elif week == 7:
        print('星期天')
        return '星期天'


# 星期几是否可以预约
def is_verify(time, li):
    week = verify_week(time)
    if week in set(li):
        print('1')
        return True
    else:
        print('2')
        return False


# 判断是否超过预约天数
def is_timing(data):
    from datetime import datetime
    # da = '2020-07-30'
    year = int(data[0:4])
    month = int(data[5:7])
    day = int(data[8:10])
    cur_day = datetime(year, month, day)
    now_year = datetime.now().year
    now_month = datetime.now().month
    now_day = datetime.now().day
    next_day = datetime(now_year, now_month, now_day)
    print(( cur_day - next_day).days)  # 1
    # print((next_day - cur_day).days)  # 1
    # return int((next_day - cur_day).days)
    # if int((cur_day - next_day).days)
    return int((cur_day - next_day).days)


# 验证身份证号和项目id是否存在
def card_project():
    pass


if __name__ == '__main__':
    # is_verify("2020-12-30",['星期一', '星期二', '星期三', '星期四', '星期五',  '星期天'])
    is_timing('2020-07-30')
